from app.problem import problemApi
from module.problems import Problems
from flask import request, jsonify, make_response
from utility.authorization import Authorization

auth = Authorization()


# 获取题目
@problemApi.route('/problems', methods=['POST'])
def get_problems():
    token = request.headers['Authorization']
    if auth.vertify_token(token):
        if request.method == 'POST':
            problems = Problems()
            data = request.get_json()
            level = data['level']
            keyword = data['keyword']
            list_data = problems.get_problems(level, keyword)
            return jsonify(list_data)
    else:
        return make_response('token失效', 403)


# 获取具体题目
@problemApi.route('/problems/<int:problem_id>', methods=['GET'])
def get_problem_detail(problem_id):
    token = request.headers['Authorization']
    if auth.vertify_token(token):
        if request.method == 'GET':
            problems = Problems()
            # print(problem_id)
            list_data = problems.get_problem_detail(problem_id)
            return jsonify(list_data)
    else:
        return make_response('token失效', 403)


# 获取样例
@problemApi.route('/examples/<int:problem_id>', methods=['GET'])
def get_examples(problem_id):
    token = request.headers['Authorization']
    if auth.vertify_token(token):
        if request.method == 'GET':
            problems = Problems()
            list_data = problems.get_examples(problem_id)
            return jsonify(list_data)
    else:
        return make_response('token失效', 403)
